package cn.zl.netty.work;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.Delimiters;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.util.CharsetUtil;

/**
 * @Description:聊天服务端的初始化器
 * @Author: zllwsy
 * @Date: 2020/10/29 9:39
 */
public class MyChatServerInitializer extends ChannelInitializer<SocketChannel> {
    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {
        socketChannel.pipeline()
                .addLast(new DelimiterBasedFrameDecoder(4096, Delimiters.lineDelimiter()))
                //编码器
                .addLast(new StringDecoder(CharsetUtil.UTF_8))
                //解码器
                .addLast(new StringEncoder(CharsetUtil.UTF_8))
                //9.在pipeline中添加自定义的Handler
                .addLast(new MyChatServerHandler());
    }
}
